﻿var zombies = zombies || {};

zombies.ViewLikeHits = function () {
    var self = this;
    self.ChartVLH = ko.observableArray();
    self.Years = ko.observableArray([
        { year: 2013 },
        { year: 2014 },
        { year: 2015 }
    ]);
    self.SelectedYear = ko.observable();
    self.Hits = ko.observable();
    self.Ads = ko.observable();
    self.Users = ko.observable();

    self.doChange = function (event) {
        loadChart(self.SelectedYear());
    }
}

var loadChart = function (year) {
    var likes = [];
    var views = [];
    var hits = [];
    $.ajax({
        url: '/api/Statistic/GetVLHStatistic/' + year,
        type: 'GET',
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    }).done(function (items) {
        statisticModel.ChartVLH.removeAll();
        for (var item in items) {
            if (items.hasOwnProperty(item)) {
                likes.push([items[item].Month, items[item].Likes === null ? 0 : items[item].Likes]);
                views.push([items[item].Month, items[item].Views === null ? 0 : items[item].Views]);
                hits.push([items[item].Month, items[item].Hits === null ? 0 : items[item].Hits]);
                statisticModel.ChartVLH.push(new zombies.Statistic_VLH(items[item]));
            }
        }
        var element = $('#chart-staticlikeviewhits');
        TebbeeChart.ViewLikeHits(views, likes, hits, element);
    }).error(function () {
        toastr.error('Lỗi gì đó');
    })
}

var loadCountAdsHitUser = function () {
    $.ajax({
        url: '/api/Statistic/getCountAdsHitUser',
        type: 'GET',
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    }).done(function (item) {
        statisticModel.Ads(item.Ads);
        statisticModel.Hits(item.Hits);
        statisticModel.Users(item.Users);
    }).error(function () {
        toastr.error('Lỗi gì đó');
    })
}

var statisticModel;
$(document).ready(function () {
    statisticModel = new zombies.ViewLikeHits();
    loadCountAdsHitUser();

    ko.applyBindings(statisticModel, document.getElementById("admin_content"));
});